<?php

/**
 * Created by PhpStorm.
 * User: CFH
 * Date: 2017/3/30
 * Time: 19:07
 */
class video_mdl extends Model
{
    public static $table = 'mac_video';


    public function add_video($id, $name,$pic,$src){
        $rs = $this->get_video_one($id);
        if(empty($rs['id'])){
            $tags = $this->dbh->insert(self::$table, array(
                'name' => $name,
                'pic' => $pic,
                'src' => $src,
                'create_time' => date('Y-m-d H:i:s')
            ));
            $rs = $this->dbh->insert_id();
        }else{
            $this->dbh->update(self::$table,
                array(
                    'name' => $name,
                    'pic' => $pic,
                    'src' => $src,
                    'create_time' => date('Y-m-d H:i:s')
                ),
                'id='.$rs['id']
            );
        }
        $rs = $this->get_video_one($id);
        return $rs;
    }


    public function get_video_one($id){
        $r = '';
        if(!empty($id)){
            $sql = 'SELECT *
					FROM '.self::$table.'
					WHERE id = '.$id;
            $r = $this->dbh->get_one($sql);
        }
        return $r;
    }


    public function get_video($pageNo,$pageSize)
    {
        $sql = "SELECT COUNT(1) count FROM " .self::$table;
        $count = $this->dbh->get_one($sql);
        $total = $count["count"];
        $sql = "SELECT * FROM " . self::$table ." ORDER BY create_time DESC LIMIT " .($pageNo-1)*$pageSize ." , ".$pageSize;
        $r = $this->dbh->get_all($sql);
        return array(
            'total' => $total,
            'list' => $r
        );
    }

    public function add_count($id,$count){
        if($this->isExitById($id)){
            $result = $this->dbh->update(self::$table, array('count'=>$count), 'id=' . $id);
        }else{
            $result = "error";
        }
        return $result;
    }

    private function isExitById($id)
    {
        $sql = "SELECT count(1) count FROM " . self::$table . ' WHERE id = ' . $id;
        $count = $this->dbh->get_one($sql);
        return (bool)$count['count'];
    }

    public function delete_video($id){
        $result = $this->dbh->delete(self::$table," id = ".$id);
        return $result;
    }

}